using UnityEngine;
using System.Collections;

public class WowadiusPlayerMovement : MonoBehaviour
{
    #region Input params
    //In degree
    public int _rotationDelta = 60;
    //In degree
    public int _rotationSpeed = 1;
    public float _moveSpeed = 0.1f;
    #endregion

    #region Private fields

    private float TranslationThreshold
    {
        get { return _moveSpeed / 100; }
    }

    private int _speedLevel = 1;
    #endregion

    // Use this for initialization
    void Start()
    {
        //Screen.showCursor = false;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        //Update speed on mouse scroll
        updateSpeed();

        //Update translation
        updateTranslation();

    }

    /// <summary>
    ///Update speed modification 
    /// </summary>
    private void updateSpeed()
    {
        int maxSpeedLevel = WowadiusGlobal.CurrentPlayerStats.MaxSpeedLevel;
        float speedModifier = Input.GetAxis("Mouse ScrollWheel");
        //Increase speed
        if (speedModifier > 0)
        {
            if (_speedLevel < maxSpeedLevel)
            {
                _moveSpeed += WowadiusGlobal.CurrentPlayerStats.SpeedStep;
                _speedLevel++;
            }
            //Decrease speed
        }
        else if (speedModifier < 0)
        {
            if (_speedLevel > 0)
            {
                _moveSpeed -= WowadiusGlobal.CurrentPlayerStats.SpeedStep;
                _speedLevel--;
            }
        }
    }

    /// <summary>
    ///Update ship translation 
    /// </summary>
    private void updateTranslation()
    {
        //Calculate mouse position
        Ray cameraToMouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit mouseHit;
        Physics.Raycast(cameraToMouseRay, out mouseHit);

        //If the mouse is not outside of the screen (in Windowed mode)
        if (mouseHit.point.x != 0 && mouseHit.point.y != 0)
        {
            //Calculate translate direction
            Vector3 moveDirection = mouseHit.point - transform.position;
            //Move to direction if haven't reached
            if (moveDirection.x >= TranslationThreshold || moveDirection.y >= TranslationThreshold || moveDirection.x <= -TranslationThreshold || moveDirection.y <= -TranslationThreshold)
            {
                //Rotate
                if (moveDirection.x > 0)
                {
                    rotateRight();
                }
                else if (moveDirection.x < 0)
                {
                    rotateLeft();
                }
                //Translate
                transform.Translate(moveDirection.normalized * _moveSpeed * Time.deltaTime);
            }
            else
            {
                //Reached, rotate back
                rotateZero();
            }

            //Always make sure the ship is on the stage plane
            transform.position = new Vector3(transform.position.x, transform.position.y, WowadiusGlobal.WorldInfo.WorldHitPlaneDistance);
        }
    }
    /// <summary>
    ///Rotate left 
    /// </summary>
    private void rotateLeft()
    {
        //TODO: Smooth rotation
        transform.eulerAngles = new Vector3(0, _rotationDelta, 0);
    }

    /// <summary>
    ///Rotate right 
    /// </summary>
    private void rotateRight()
    {
        //TODO: Smooth rotation
        transform.eulerAngles = new Vector3(0, -_rotationDelta, 0);
    }

    /// <summary>
    ///Rotate the ship back to origin 
    /// </summary>
    private void rotateZero()
    {
        //TODO: Smooth rotation
        transform.eulerAngles = Vector3.zero;
    }
}
